<?php
namespace app\models;
use yii\base\Object;

/**
 *
 * @author Brent Shaffer <bshafs at gmail dot com>
 */
class Oauthtoken extends Object
{
    protected $tokenStorage;
    protected $refreshStorage;
    protected $config;

    /**
     * @param OAuth2\Storage\AccessTokenInterface  $tokenStorage   REQUIRED Storage class for saving access token information
     * @param OAuth2\Storage\RefreshTokenInterface $refreshStorage OPTIONAL Storage class for saving refresh token information
     * @param array                                $config         OPTIONAL Configuration options for the server
     *                                                             <code>
     *                                                             $config = array(
     *                                                             'token_type' => 'bearer',              // token type identifier
     *                                                             'access_lifetime' => 3600,             // time before access token expires
     *                                                             'refresh_token_lifetime' => 1209600,   // time before refresh token expires
     *                                                             );
     *                                                             </endcode>
     */
   public function __construct($tokenStorage=true, $refreshStorage=null,$config = array())
    {
        $this->tokenStorage = $tokenStorage;
         $this->refreshStorage = $refreshStorage;

        $this->config = array_merge(array(
            'token_type'             => 'bearer',
            'access_lifetime'        => 7200,
            'refresh_token_lifetime' => 1209600,
        ), $config);
    } 

    /**
     * Handle the creation of access token, also issue refresh token if supported / desirable.
     *
     * @param $client_id                client identifier related to the access token.
     * @param $user_id                  user ID associated with the access token
     * @param $scope                    OPTIONAL scopes to be stored in space-separated string.
     * @param bool $includeRefreshToken if true, a new refresh_token will be added to the response
     *
     * @see http://tools.ietf.org/html/rfc6749#section-5
     * @ingroup oauth2_section_5
     */
    public function createAccessToken($client_id, $user_id, $scope = null, $includeRefreshToken = true)
    {
        $token = array(
            "access_token" => $this->generateAccessToken(),
            "expires_time" => $this->config['access_lifetime'],
        );

        return $token;
    }

    /**
     * Generates an unique access token.
     *
     * Implementing classes may want to override this function to implement
     * other access token generation schemes.
     *
     * @return
     * An unique access token.
     *
     * @ingroup oauth2_section_4
     */
    protected function generateAccessToken()
    {
        if (function_exists('mcrypt_create_iv')) {
            $randomData = mcrypt_create_iv(40, MCRYPT_DEV_URANDOM);
            if ($randomData !== false && strlen($randomData) === 40) {
                return bin2hex($randomData);
            }
        }
        if (function_exists('openssl_random_pseudo_bytes')) {
            $randomData = openssl_random_pseudo_bytes(20);
            if ($randomData !== false && strlen($randomData) === 20) {
                return bin2hex($randomData);
            }
        }
        if (@file_exists('/dev/urandom')) { // Get 100 bytes of random data
            $randomData = file_get_contents('/dev/urandom', false, null, 0, 20);
            if ($randomData !== false && strlen($randomData) === 20) {
                return bin2hex($randomData);
            }
        }
        // Last resort which you probably should just get rid of:
        $randomData = mt_rand() . mt_rand() . mt_rand() . mt_rand() . microtime(true) . uniqid(mt_rand(), true);

        return substr(hash('sha512', $randomData), 0, 40);
    }
}
